package net.gz01.wallet.web.controller.admin;

import java.math.BigDecimal;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.config.SystemEnum;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Channel;
import net.gz01.wallet.web.model.ClearingDetail;
import net.gz01.wallet.web.model.TradeOrder;

public class ClearingDetailController extends BaseController {
	/**
	 * 查询本地差错
	 */
	public void searchLocalError() {
		ZOSearchCondition sc = super.getSearchCondition();
		sc.setParameter("compareStatus1", SystemEnum.CompareStatus.LocalError.getValue());
		sc.setParameter("compareStatus2", SystemEnum.CompareStatus.LocalErrorSuccess.getValue());
		setPageResultToRequest("items", ClearingDetail.dao.search(sc));
		render("clearing/detail/localErrorList.html");
	}

	/**
	 * 本地差错处理页面
	 */
	public void localErrorHandlePage() {
		setAttr("channelList",  Channel.dao.selectAll());
		setAttr("clearingDetail",  ClearingDetail.dao.findById(getPara("id")));
		render("clearing/detail/localErrorHandle.html");
	}

	/**
	 * 本地差错处理
	 */
	public void localErrorHandle() {
		ClearingDetail clearingDetail = getModel(ClearingDetail.class);
		clearingDetail.update();
		renderSuccess();
	}

	/**
	 * 查询渠道差错
	 */
	public void searchChannelError() {
		ZOSearchCondition sc = super.getSearchCondition();
		sc.setParameter("compareStatus1", SystemEnum.CompareStatus.ChannelError.getValue());
		sc.setParameter("compareStatus2", SystemEnum.CompareStatus.ChannelErrorSuccess.getValue());
		Page<ClearingDetail> pageResult = ClearingDetail.dao.search(sc);
		setPageResultToRequest("items", pageResult);
		render("clearing/detail/channelErrorList.html");
	}

	/**
	 * 渠道差错处理页面
	 */
	public void channelErrorHandlePage() {
		ClearingDetail clearingDetail = ClearingDetail.dao.findById(getPara("id"));
		TradeOrder virtualOrder = TradeOrder.dao.selectByOrderNo(clearingDetail.getOrderNo());
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("clearingDetail", clearingDetail);
		setAttr("virtualOrder", virtualOrder);
		render("clearing/detail/channelErrorHandle.html");
	}

	/**
	 * 渠道差错处理
	 */
	public void channelErrorHandle() {
		String feeStr = getPara("fee");
		BigDecimal fee = new BigDecimal(feeStr).multiply(new BigDecimal("100"));
		ClearingDetail detail = getModel(ClearingDetail.class);
		detail.setFee(fee.intValue());
		detail.update();
		renderSuccess();
	}

	/**
	 * 明细风控查询
	 */
	public void detailRiskSearch() {
		ZOSearchCondition sc = super.getSearchCondition();
		sc.setParameter("isRisk", "true");
		Page<ClearingDetail> pageResult = ClearingDetail.dao.search(sc);
		setPageResultToRequest("items", pageResult);
		render("clearing/detail/detailRiskList.html");
	}

	/**
	 * 明细冻结页面
	 */
	public void detailFreezePage() {
		render("clearing/detail/detailFreeze.html");
	}

	/**
	 * 明细冻结
	 */
	public void detailFreeze() {
		ClearingDetail detail = ClearingDetail.dao.selectFreezeOrderByOrderNo(getPara("clearingDetail.orderNo"));
		if (detail == null) {
			renderFail();
		}else {
			detail.setIsRisk(true);
			detail.setPaymentStatus(SystemEnum.PaymentStatus.Freeze.getValue());
			detail.setFreezeTime(ZODate.getDateTime());
			detail.update();
			renderSuccess();
		}
	}

	/**
	 * 明细解冻页面
	 */
	public void detailThawPage() {
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("clearingDetail", ClearingDetail.dao.findById(getPara("id")));
		render("clearing/detail/detailThaw.html");
	}

	/**
	 * 明细解冻
	 */
	public void detailThaw() {
		ClearingDetail clearingDetail = getModel(ClearingDetail.class);
		clearingDetail.setPaymentStatus(SystemEnum.PaymentStatus.Thaw.getValue());
		clearingDetail.setThawTime(ZODate.getDateTime());
		clearingDetail.update();
		renderSuccess();
	}

	/**
	 * 查询明细
	 */
	public void searchDetail() {
		Page<ClearingDetail> pageResult = ClearingDetail.dao.search(super.getSearchCondition());
		List<Object> list = ClearingDetail.dao.searchSum(super.getSearchCondition());
		Object[] value = (Object[]) list.get(0);
		setAttr("sumCount", value[0]);
		setAttr("sumAmount", value[1] == null ? "0" : value[1]);
		setAttr("sumFee", value[2] == null ? "0" : value[2]);
		setPageResultToRequest("items", pageResult);
		render("clearing/detail/detailList.html");
	}

	/**
	 * 查看明细
	 */
	public void viewDetail() {
		ClearingDetail clearingDetail = ClearingDetail.dao.findById(getPara("id"));
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("clearingDetail", clearingDetail);
		render("clearing/detail/detailView.html");
	}

}
